﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Mechanical;
using YArchitech.Controls;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using YArchitech.Revit;

namespace YArchitech.Plumbing
{
	public partial class DuctTypeMagForm : System.Windows.Forms.Form
	{
		public DuctTypeMagForm(Autodesk.Revit.DB.Document doc, string typeNmae)
		{
			this.InitializeComponent();
			new YJKProcessTools().ShowInTaskbarChange(this);
			this.doc = doc;
			this.type = typeNmae;
			this.comboBox1.Items.Add("矩形");
			this.comboBox1.Items.Add("圆形");
			this.comboBox1.Items.Add("椭圆形");
			this.initLstPipeType(typeNmae);
			this.Text = "风管类型管理";
			base.Icon = Resource.YArchitechIcon;
			this.btnNew.Image = Resources.AddNew16;
			this.btnEdit.Image = Resources.modify16;
			this.btnDelete.Image = Resources.delete16;
		}

		private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
		{
			this.lastCombox = this.comboBox1.Text;
			if (this.isInit)
			{
				this.lsbPipeType.Items.Clear();
				if (this.comboBox1.SelectedItem.ToString() == "矩形")
				{
					using (List<Element>.Enumerator enumerator = DuctTypeMagForm.listJuxing.GetEnumerator())
					{
						while (enumerator.MoveNext())
						{
							Element element = enumerator.Current;
							ListItem item = new ListItem(element.Name, element.Id.IntegerValue.ToString());
							this.lsbPipeType.Items.Add(item);
						}
						goto IL_211;
					}
				}
				if (this.comboBox1.SelectedItem.ToString() == "圆形")
				{
					using (List<Element>.Enumerator enumerator = DuctTypeMagForm.listyuanxing.GetEnumerator())
					{
						while (enumerator.MoveNext())
						{
							Element element2 = enumerator.Current;
							ListItem item2 = new ListItem(element2.Name, element2.Id.IntegerValue.ToString());
							this.lsbPipeType.Items.Add(item2);
						}
						goto IL_211;
					}
				}
				if (this.comboBox1.SelectedItem.ToString() == "椭圆形")
				{
					using (List<Element>.Enumerator enumerator = DuctTypeMagForm.listtuoyuan.GetEnumerator())
					{
						while (enumerator.MoveNext())
						{
							Element element3 = enumerator.Current;
							ListItem item3 = new ListItem(element3.Name, element3.Id.IntegerValue.ToString());
							this.lsbPipeType.Items.Add(item3);
						}
						goto IL_211;
					}
				}
				foreach (Element element4 in DuctTypeMagForm.listJuxing)
				{
					ListItem item4 = new ListItem(element4.Name, element4.Id.IntegerValue.ToString());
					this.lsbPipeType.Items.Add(item4);
				}
				IL_211:
				if (this.lsbPipeType.Items.Count > 0 && this.lsbPipeType.Items[0] != null)
				{
					this.lsbPipeType.SelectedItem = this.lsbPipeType.Items[0];
				}
			}
		}

		private void initLstPipeType(string typeNmae)
		{
			DuctTypeMagForm.listJuxing.Clear();
			DuctTypeMagForm.listyuanxing.Clear();
			DuctTypeMagForm.listtuoyuan.Clear();
			new List<Element>();
			List<Element> elementByClass = YJKElementFilter.GetElementByClass(typeof(DuctType), this.doc);
			this.lsbPipeType.DisplayMember = "Name";
			this.lsbPipeType.ValueMember = "Value";
			foreach (Element element in elementByClass)
			{
				DuctType ductType = element as DuctType;
				Duct duct = Duct.Create(this.doc, YJKElementFilter.GetElementByClass(typeof(MechanicalSystemType), this.doc).ElementAt(0).Id, ductType.Id, YJKElementFilter.GetElementByClass(typeof(Level), this.doc).ElementAt(0).Id, new XYZ(111.0, 111.0, 111.0), new XYZ(222.0, 222.0, 222.0));
				YJKPiping<Duct> hypiping = new YJKPiping<Duct>(duct);
                if ((int)hypiping.StartConnector.Connector.Shape == 1)
				{
					DuctTypeMagForm.listJuxing.Add(element);
				}
                else if ((int)hypiping.StartConnector.Connector.Shape == null)
				{
					DuctTypeMagForm.listyuanxing.Add(element);
				}
                else if ((int)hypiping.StartConnector.Connector.Shape == 2)
				{
					DuctTypeMagForm.listtuoyuan.Add(element);
				}
				this.doc.DeleteElement(duct);
			}
			this.comboBox1.SelectedItem = this.lastCombox;
			this.isInit = true;
			this.lsbPipeType.Items.Clear();
			if (this.comboBox1.SelectedItem.ToString() == "矩形")
			{
				using (List<Element>.Enumerator enumerator = DuctTypeMagForm.listJuxing.GetEnumerator())
				{
					while (enumerator.MoveNext())
					{
						Element element2 = enumerator.Current;
						ListItem item = new ListItem(element2.Name, element2.Id.IntegerValue.ToString());
						this.lsbPipeType.Items.Add(item);
					}
					goto IL_3AC;
				}
			}
			if (this.comboBox1.SelectedItem.ToString() == "圆形")
			{
				using (List<Element>.Enumerator enumerator = DuctTypeMagForm.listyuanxing.GetEnumerator())
				{
					while (enumerator.MoveNext())
					{
						Element element3 = enumerator.Current;
						ListItem item2 = new ListItem(element3.Name, element3.Id.IntegerValue.ToString());
						this.lsbPipeType.Items.Add(item2);
					}
					goto IL_3AC;
				}
			}
			if (this.comboBox1.SelectedItem.ToString() == "椭圆形")
			{
				using (List<Element>.Enumerator enumerator = DuctTypeMagForm.listtuoyuan.GetEnumerator())
				{
					while (enumerator.MoveNext())
					{
						Element element4 = enumerator.Current;
						ListItem item3 = new ListItem(element4.Name, element4.Id.IntegerValue.ToString());
						this.lsbPipeType.Items.Add(item3);
					}
					goto IL_3AC;
				}
			}
			foreach (Element element5 in DuctTypeMagForm.listJuxing)
			{
				ListItem item4 = new ListItem(element5.Name, element5.Id.IntegerValue.ToString());
				this.lsbPipeType.Items.Add(item4);
			}
			IL_3AC:
			if (this.lsbPipeType.Items.Count > 0 && this.lsbPipeType.Items[0] != null)
			{
				this.lsbPipeType.SelectedItem = this.lsbPipeType.Items[0];
			}
		}

		private void btnOK_Click(object sender, EventArgs e)
		{
			base.DialogResult = DialogResult.OK;
		}

		private void btnDelete_Click(object sender, EventArgs e)
		{
			if (MessageBox.Show("删除类型后，所有此类型的管道也将被删除，确定要删除吗？", "盈建科提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes)
			{
				return;
			}
			if (this.lsbPipeType.SelectedItem != null)
			{
				try
				{
					this.doc.Delete(new ElementId(int.Parse((this.lsbPipeType.SelectedItem as ListItem).Value)));
					this.doc.Regenerate();
					this.initLstPipeType(this.type);
				}
				catch (Exception)
				{
					YJKMessageBox.Information("删除失败，管道至少存在一种类型");
				}
			}
		}

		private void btnEdit_Click(object sender, EventArgs e)
		{
			if (this.lsbPipeType.SelectedItem != null)
			{
				ElementId elementId = new ElementId(int.Parse((this.lsbPipeType.SelectedItem as ListItem).Value));
				Element elementById = this.doc.GetElementById(elementId);
				EditDuctTypeForm editDuctTypeForm = new EditDuctTypeForm(this.doc, elementById, this.lsbPipeType);
				DialogResult dialogResult = editDuctTypeForm.ShowDialog();
				if (DialogResult.OK == dialogResult)
				{
					string pipeTypeName = editDuctTypeForm.pipeTypeName;
					elementById.Name = pipeTypeName;
					this.doc.Regenerate();
					this.initLstPipeType(this.type);
					this.SelectedItemByName(pipeTypeName);
				}
			}
		}

		private void btnNew_Click(object sender, EventArgs e)
		{
			if (this.lsbPipeType.SelectedItem != null)
			{
				ElementId elementId = new ElementId(int.Parse((this.lsbPipeType.SelectedItem as ListItem).Value));
				Element elementById = this.doc.GetElementById(elementId);
				NewDuctTypeForm newDuctTypeForm = new NewDuctTypeForm(this.doc, elementById, this.lsbPipeType);
				DialogResult dialogResult = newDuctTypeForm.ShowDialog();
				if (DialogResult.OK == dialogResult)
				{
					this.initLstPipeType(this.type);
					string pipeTypeName = newDuctTypeForm.pipeTypeName;
					this.SelectedItemByName(pipeTypeName);
				}
			}
		}

		private void SelectedItemByName(string name)
		{
			foreach (object obj in this.lsbPipeType.Items)
			{
				ListItem listItem = (ListItem)obj;
				if (name == listItem.Name)
				{
					this.lsbPipeType.SelectedItem = listItem;
					break;
				}
			}
		}

		private Document doc;

		private string type = "";

		private bool isInit;

		public static List<Element> listJuxing = new List<Element>();

		public static List<Element> listyuanxing = new List<Element>();

		public static List<Element> listtuoyuan = new List<Element>();

		private string lastCombox = "矩形";
	}
}
